

#import "WBMainController.h"
#import "WBRootController.h"
#import "WBComposeController.h"

@interface WBMainController ()

@end

@implementation WBMainController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    
    NSURL *url = [[NSBundle mainBundle ] URLForResource:@"main.json" withExtension:nil ];
    
    NSData  *data = [NSData dataWithContentsOfURL:url];
    
    NSArray *dicArr = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    
    //创建一个子控制器的数组
    NSMutableArray<UINavigationController *> *controllers = [NSMutableArray array];
    
    for (NSDictionary *item in dicArr) {
        UINavigationController *nav = [self addChildControllerWithParameter:item];
        [controllers addObject:nav];
    }
    //设置tabBarController的子控制器
    self.viewControllers = controllers;
    
    //添加发布按钮
    [self addComposeButton];
}

-(UINavigationController *)addChildControllerWithParameter:(NSDictionary *) parameters {
    
    NSString *clsName = parameters[@"clsName"];
    
    Class class = NSClassFromString(clsName);
    
    //用类来创建一个对象
    WBRootController *vc = [[class alloc] init];
    //设置title
    vc.title = parameters[@"title"];
    
    //设置tabBarItem的图标
    NSString *imageName = parameters[@"imageName"];
    NSString *originalImage = [NSString stringWithFormat:@"tabbar_%@",imageName];
    NSString *selectImageName = [NSString stringWithFormat:@"%@_selected",originalImage];
    //设置tabbarItem的图标
    vc.tabBarItem.image = [[UIImage imageNamed:originalImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    NSDictionary *attrDict = @{NSFontAttributeName: [UIFont systemFontOfSize:10],NSForegroundColorAttributeName: [UIColor grayColor]};
    NSDictionary *attrSel = @{NSFontAttributeName: [UIFont systemFontOfSize:10],NSForegroundColorAttributeName: [UIColor orangeColor]};
    
    //设置tabbar的title的文本属性
    [vc.tabBarItem setTitleTextAttributes:attrDict forState:UIControlStateNormal];
    [vc.tabBarItem setTitleTextAttributes:attrSel forState:UIControlStateSelected];
    
    vc.visitorDic = parameters[@"visitorInfo"];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    
    return nav;
}

-(void)addComposeButton {
    
    UIButton *composeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    NSString *bgImageName = @"tabbar_compose_button";
    NSString *highlightBgImageName = [NSString stringWithFormat:@"%@_highlighted",bgImageName];
    [composeBtn setBackgroundImage:[UIImage imageNamed:bgImageName] forState:UIControlStateNormal];
    [composeBtn setBackgroundImage:[UIImage imageNamed:highlightBgImageName] forState:UIControlStateHighlighted];
    NSString *imageName = @"tabbar_compose_icon_add";
    NSString *highlightImageName = [NSString stringWithFormat:@"%@_highlighted",imageName];
    [composeBtn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [composeBtn setImage:[UIImage imageNamed:highlightImageName] forState:UIControlStateHighlighted];
    
    [composeBtn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    
     composeBtn.frame = CGRectMake(0, 0, self.tabBar.bounds.size.width * 0.2, self.tabBar.bounds.size.height);
    composeBtn.center = CGPointMake(self.tabBar.bounds.size.width * 0.5, self.tabBar.bounds.size.height * 0.5);
     [self.tabBar insertSubview:composeBtn atIndex:0];
   
    [self.tabBar bringSubviewToFront:composeBtn];
   
}

-(void)click{
    NSLog(@"弹出发布按钮");
    WBComposeController *composeVC = [[WBComposeController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:composeVC];
    [self presentViewController:nav animated:YES completion:nil];
    
}
@end
